<template>
  <div>
    <el-table :data="records" style="width: 100%; margin: 10px 0" border>
      <!-- 序号 -->
      <el-table-column
        prop="prop"
        label="序号"
        width="80"
        align="center"
        type="index"
      ></el-table-column>
      <!-- 名称 -->
      <el-table-column
        header-align="center"
        align="center"
        prop="skuName"
        label="名称"
      >
      </el-table-column>
      <!-- 描述 -->
      <el-table-column
        header-align="center"
        align="center"
        prop="skuDesc"
        label="描述"
      >
      </el-table-column>
      <!-- 默认图片 -->
      <el-table-column
        header-align="center"
        align="center"
        label="默认图片"
        width="110"
      >
        <template slot-scope="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <!-- 重量 -->
      <el-table-column
        header-align="center"
        width="80"
        align="center"
        prop="weight"
        label="重量"
      >
      </el-table-column>

      <!-- 价格 -->
      <el-table-column
        header-align="center"
        width="80"
        align="center"
        prop="price"
        label="价格"
      >
      </el-table-column>

      <!-- 操作 -->
      <el-table-column header-align="center" label="操作">
        <template slot-scope="{ row, $index }">
          <el-button
            type="success"
            icon="el-icon-sort-up"
            size="mini"
            title="上架"
            v-if="row.isSale == 0"
            @click="sale(row)"
          ></el-button>
          <el-button
            icon="el-icon-sort-down"
            size="mini"
            v-else
            title="下架"
            @click="cancel(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="$message('正在开发中...')"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="total"
      align="center"
      background
    >
    </el-pagination>

    <!-- 抽屉效果 -->
    <el-drawer
      title="我是标题"
      size="40%"
      :visible.sync="show"
      :with-header="false"
    >
      <!-- 第一行 -->
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">名称</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            {{ skuInfo.skuName }}
          </div></el-col
        >
      </el-row>
      <!-- 第二行 -->
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">描述</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            {{ skuInfo.skuDesc }}
          </div></el-col
        >
      </el-row>
      <!-- 第三行 -->
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">价格</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            {{ skuInfo.price }}元
          </div></el-col
        >
      </el-row>
      <!-- 第四行 -->
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">平台属性</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            <el-tag
              type="success"
              v-for="(attr, index) in skuInfo.skuAttrValueList"
              :key="attr.id"
              style="margin-right: 10px"
              >{{ attr.attrId }}-{{ attr.valueId }}</el-tag
            >
          </div></el-col
        >
      </el-row>
      <!-- 第五行 -->
      <el-row>
        <el-col :span="5"
          ><div class="grid-content bg-purple">商品图片</div></el-col
        >
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img :src="item.imgUrl" />
              </el-carousel-item>
            </el-carousel></div
        ></el-col>
      </el-row>
    </el-drawer>
  </div>
</template>


<script>
export default {
  name: "Sku",

  data() {
    return {
      page: 1, //代表当前第几页
      limit: 10, //代表当前页面有几条数据
      records: [], //存储SKU列表的数据
      total: 0, //存储分页器一共展示的数据
      skuInfo: {}, //存储SKU信息
      show: false,
    };
  },
  methods: {
    //请求sku数据
    async getSkuList(pages = 1) {
      this.page = pages;
      let result = await this.$API.sku.reqSkuList(this.page, this.limit);
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
      console.log(result);
    },
    //每页最大显示数量发生变化触发
    handleSizeChange(size) {
      this.limit = size;
      this.getSkuList();
    },
    //当前页发生改变时触发
    handleCurrentChange(page) {
      this.page = page;
      this.getSkuList(page);
    },
    //上架操作
    async sale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message({
          type: "success",
          message: "上架成功",
        });
      }
    },
    //下架操作
    async cancel(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message({
          type: "success",
          message: "下架成功",
        });
      }
    },
    //抽屉效果
    async getSkuInfo(sku) {
      //展示抽屉
      this.show = true;
      //获取SKU数据
      let result = await this.$API.sku.reqSkuById(sku.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
  },

  mounted() {
    this.getSkuList();
  },
};
</script>


<style >
.bg-purple {
  float: right;
  font-size: 20px;
}

.el-drawer__body .el-row:nth-child(1) .grid-content {
  margin-top: 80px;
}

.grid-content {
  margin-top: 20px;
}

.grid-content img {
  width: 100%;
  height: 100%;
}
.bg-purple-light {
  padding-left: 10px;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>